package com.huyn.baseframework;

import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.SysUtil;

/**
 * See https://help.aliyun.com/document_detail/44688.html?spm=5176.doc44688.6.932.XMK8IK
 * Jan 6th 2017
 * 阿里云的算法更新
 * Created by huyaonan on 16/10/8.
 */
public class ImageSizeDefineUtil {

    private static final String ALIYUN = "?x-oss-process=image/";

    public static String getImgUrl(String url, int w, int h) {
        if(StringUtils.isBlank(url))
            return "";
        if(!url.contains("aube-tv"))
            return url;
        return getImgUrl(url, w, h, 0, 0);
    }

    public static String getImgUrl(int w, int h) {
        return getImgUrl(w, h, 0, 0);
    }

    /**
     * @param w
     * @param h
     * @param radius
     * @param sigma
     * @return ?x-oss-process=image/resize,m_lfit,h_100,w_100
     * Deprecated @100w_100h_1e_1c_3-2bl
     */
    public static String getImgUrl(int w, int h, int radius, int sigma) {
        StringBuffer sb = new StringBuffer();
        sb.append("?x-oss-process=image/resize,m_lfit,h_").append(h).append(",w_").append(w);
        if(radius == 0 || sigma == 0)
            return sb.toString();
        return sb.append("/blur,r_").append(radius).append(",s_").append(sigma).toString();
    }

    public static String getImgUrl(String url, int w, int h, int radius, int sigma) {
        if(w == 0 || h == 0)
            return url;
        if(StringUtils.isBlank(url))
            return "";
        StringBuffer sb = new StringBuffer();
        if(url.contains(ALIYUN))
            sb.append(url.substring(0, url.indexOf(ALIYUN)));
        else
            sb.append(url);
        sb.append("?x-oss-process=image/resize,m_lfit,h_").append(h).append(",w_").append(w);
        if(radius == 0 || sigma == 0)
            return sb.toString();
        String result =sb.append("/blur,r_").append(radius).append(",s_").append(sigma).toString();
        return result;
    }

    public static String getPicList(String url) {
        if(StringUtils.isBlank(url))
            return "";
        if (com.android.volley.toolbox.Scheme.ofUri(url) != com.android.volley.toolbox.Scheme.HTTP) {
            return url;
        }

        if(!url.contains("aube-tv"))
            return url;

        if(url.indexOf(ALIYUN) != -1)
            url = url.substring(0, url.indexOf(ALIYUN));
        String size;

        if(SysUtil.isXXLargeScreen(null)) {
            size = getImgUrl(600, 600);
        } else if(SysUtil.isXLargeScreen(null)) {
            size = getImgUrl(400, 400);
        } else {
            size = getImgUrl(300, 300);
        }

        return url + size;
    }

    public static String getSquareUrl(String url, int width, int height) {
        if(StringUtils.isBlank(url))
            return "";
        if (com.android.volley.toolbox.Scheme.ofUri(url) != com.android.volley.toolbox.Scheme.HTTP) {
            return url;
        }
        if(url.indexOf(ALIYUN) != -1)
            url = url.substring(0, url.indexOf(ALIYUN));
        return new StringBuffer().append(url).append("?x-oss-process=image/resize,m_fill,h_").append(height).append(",w_").append(width).toString();
    }

    public static String getGalleryPicArg(String url) {
        if(StringUtils.isBlank(url))
            return "";
        if (com.android.volley.toolbox.Scheme.ofUri(url) != com.android.volley.toolbox.Scheme.HTTP) {
            return url;
        }

        if(!url.contains("aube-tv"))
            return url;

        if(url.indexOf(ALIYUN) != -1)
            url = url.substring(0, url.indexOf(ALIYUN));
        return url + getGalleryPicArg();
    }

    public static String getGalleryPicArg() {
        if(SysUtil.isXXLargeScreen(null)) {
            return getImgUrl(600, 600);
        } else {
            return getImgUrl(400, 400);
        }
    }

    public static String appendHeaderLogo(String url) {
        if(StringUtils.isBlank(url))
            return "";
        if(!url.contains("aube-tv"))
            return url;
        if(url.indexOf(ALIYUN) != -1)
            url = url.substring(0, url.indexOf(ALIYUN));
        return url + getImgUrl(100, 100);
    }

}